package com.snack.util;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @Title. bean的操作类<br>
 * @Description.
 * <p>
 * @Copyright: Copyright (c) Apr 30, 2009 2:40:47 PM
 * <p>
 * @Company: 北京宽连十方数字技术有限公司
 * <p>
 * @Author: Lingf
 * <p>
 * @Version: 1.0
 * <p>
 * @History: 
 * <p>
 */
public class BeanUtil {
	private static final Log logger = LogFactory.getLog(BeanUtil.class);
	
	/**
	 * 将javabean中参数值为null，则转化为字符串“”
	 * @param src
	 */
	public static void nullFilter(Object src) {
		if (src == null) {
			return;
		}

		Method[] srcMethods = src.getClass().getMethods();
		for (Method getMethod : srcMethods) {
			String getMethodName = getMethod.getName();
			String setMethodName = "";
			if (getMethodName.startsWith("get") && getMethod.getReturnType().equals(String.class)) {
				try {
					if (getMethod.invoke(src, new Object[0]) == null) { // 当属性值为null时，将null转换成空字符串
						setMethodName = "set" + getMethodName.substring(3); // 拼装setter
						Method setMethod = src.getClass().getMethod(setMethodName, getMethod.getReturnType());
						setMethod.invoke(src, "");
					}
				} catch (Exception ex) {
					logger.error("BeanUtil -> nullFilter", ex);
				}
			}
		}
	}	
}
